package com.saicmotor.paging;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.saicmotor.paging.Bean.DataBean;

/**
 * @Author xinxinzhang
 * @Date 2022/08/17
 **/
public class PagingAdapter extends PagedListAdapter<DataBean.ResultBean, PagingAdapter.ViewHolder> {

    public PagingAdapter() {
        super(itemCallback);
    }

    @NonNull
    @Override
    public PagingAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycleview, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PagingAdapter.ViewHolder holder, int position) {
        DataBean.ResultBean bean = getItem(position);
        if (bean != null) {
            holder.desc.setText(bean.getDesc());
            holder.time.setText(bean.getPublishedAt());
            holder.type.setText(bean.getType());
            holder.auth.setText(bean.getWho());
        }
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView desc;
        TextView time;
        TextView type;
        TextView auth;

        public ViewHolder(View itemView) {
            super(itemView);
            desc = itemView.findViewById(R.id.desc);
            time = itemView.findViewById(R.id.time);
            type = itemView.findViewById(R.id.type);
            auth = itemView.findViewById(R.id.auth);
        }
    }


    private static DiffUtil.ItemCallback<DataBean.ResultBean> itemCallback = new DiffUtil.ItemCallback<DataBean.ResultBean>() {
        @Override
        public boolean areItemsTheSame(@NonNull DataBean.ResultBean oldItem, @NonNull DataBean.ResultBean newItem) {
            return oldItem.getGanhuo_id().equals(newItem.getGanhuo_id());
        }

        @SuppressLint("DiffUtilEquals")
        @Override
        public boolean areContentsTheSame(@NonNull DataBean.ResultBean oldItem, @NonNull DataBean.ResultBean newItem) {
            return oldItem.equals(newItem);
        }
    };

}
